﻿using System.Web.Mvc;
using FriendStar.Code;
using FriendStar.Application.BaseData;
using System.Collections.Generic;

namespace FriendStar.Web.Areas.BaseData.Controllers
{
    public class EquipmentArchiveController : ControllerBase
    {
        private string UseUnit = Configs.GetValue("UseUnit");

        EquipmentArchiveApp app = new EquipmentArchiveApp();


        public ActionResult EquipmentArchiveManagement()
        {
            ViewData["useUnit"] = Configs.GetValue("UseUnit");

            return View();
        }

        public ActionResult EquipmentArchiveAdd(string id)
        {
            if (null != id)
            {
                ViewData["jsonData"] = app.GetEquipmentArchive(id).ToJson();
            }
            else
            {
                ViewData["jsonData"] = "null";
            }

            return View();
        }



        public ActionResult GetEquipmentArchivies(Pagination pagination, string EquipNO, string EquipModel, string EquipName, string EquipUnit, string EquipWorkshop, string ManuUnit)
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = new
            {
                rows = app.GetEquipmentArchivies(pagination, EquipNO, EquipModel, EquipName, EquipUnit, EquipWorkshop, ManuUnit, useUnit),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        public ActionResult DeleteEquipmentArchive(string id)
        {
            var data = app.DeleteEquipmentArchive(id);

            return Content(data.ToJson());
        }

        public ActionResult UpdateEquipmentArchive(string param)
        {
            string useUnit = Configs.GetValue("UseUnit");

            string curUser = OperatorProvider.Provider.GetCurrent().UserName;

            System.DateTime now = System.DateTime.Now;

            Dictionary<string, object> item = new Dictionary<string, object>();

            item = param.ToObject<Dictionary<string, object>>();

            object data = null;

            item.Add("UseUnit", useUnit);

            if (item["EquipID"].Equals(string.Empty))
            {
                string guid = System.Guid.NewGuid().ToString();

                item["EquipID"] = guid;

                item["EquipNO"] = string.Format("{0}{1}", item["EquipNO"], useUnit);

                item.Add("InputUser", curUser);

                item.Add("InputDate", now.ToString("yyyy-MM-dd"));

                item.Add("InputTime", now.ToString("HH:mm:ss"));

                data = app.InsertEquipmentArchive(item);
            }
            else
            {
                item.Add("FUpdateBy", curUser);

                item.Add("FUpdateDate", now.ToString("yyyy-MM-dd"));

                item.Add("FUpdateTime", now.ToString("HH:mm:ss"));

                data = app.UpdateEquipmentArchive(item);
            }

            return Content(data.ToJson());
        }

        public ActionResult GetUseUnitData()
        {
            string useUnit = Configs.GetValue("UseUnit");

            var data = app.GetUseUnitData(useUnit);

            return Content(data.ToJson());
        }

        public ActionResult ChangeEquipmentArchive(string type, string ids)
        {
            string curUser = OperatorProvider.Provider.GetCurrent().UserName;

            System.DateTime now = System.DateTime.Now;

            var data = app.ChangeEquipmentArchive(type, ids, curUser, now);

            return Content(data.ToJson());
        }

    }
}
